package com.maven.sort;

import java.util.Arrays;

/**
 * @Author YongQiang
 * @Date 2021/8/13 14:19
 * @Version 1.0
 */
public class ShellSort {
    public static void main(String[] args) {
        int[] arr = {12, 45, 34, 24, 85, 5, 31, 45, 76, 5, 17, 75, 28};
        //swap(arr, 2, 3);
        sort(arr);
        System.out.println(Arrays.toString(arr));
    }

    public static void sort(int[] arr) {
        if (arr.length < 2) {
            return;
        }

        //外围控制循环次数
        for (int i = 0; i < arr.length; i++) {
            // 内层控制每一次的冒泡
            for (int j = 1; j < arr.length - i; j++) {
                if (arr[j - 1] > arr[j]) {
                    swap(arr, j - 1, j);
                }
            }
        }
    }

    public static void swap(int[] arr, int a, int b) {
        if (a == b) {
            return;
        }
        arr[a] = arr[a] ^ arr[b];
        arr[b] = arr[a] ^ arr[b];
        arr[a] = arr[a] ^ arr[b];
    }
}
